<?php
class SlidersController extends AppController
{
	var $name = 'Sliders';
	var $helpers = array('Html','Form','Sliders.Sliders','Cms.Index');
	var $uses = array('Sliders.Slider','Sliders.Zone');
	
	function beforeFilter()
	{
		if(cr('cmsInstalled'))
		{
			cri('Sliders.Zone')->init();
		}
		parent::beforeFilter();
	}
	function admin_index()
	{
		$this->pageTitle = __("Sliders");
		$this->paginate['limit'] = 10;
		$this->paginate['order'] = array('created DESC');
		$this->paginate['conditions'] = array('Slider.zone'=>$this->Slider->_getZoneOptionList()) ;
		$slidersData = $this->paginate();
		$this->set(compact('slidersData'));
	}

	function admin_add()
	{
		$zoneList = $this->Slider->_getZoneOptionList();
		$this->Slider->setFieldProperties('zone',array('options'=>$zoneList));
		if(empty($zoneList))
		{
			$this->warningMsg(__('No zone(s) exists. Please define some slider zones first.'));
			$this->redirect('/admin/sliders/editZone');
		}
		$this->pageTitle = __("New Slider");
		if(!empty($this->data))
		{
			$this->Slider->create();
                        $this->_processBanner();
			if($this->Slider->save($this->data))
			{
				$this->successMsg(__('Slider has been saved successfully!'));
				$this->redirect('/admin/sliders/index');
			}
			else
			{
				$this->errorMsg(__('Slider has not been saved. Try again!'));
			}
		}else
		{
			$this->Slider->setFieldProperties('published',array('checked'=>true));
		}	
	}

	function admin_edit($id = null)
	{		
		$zoneList = $this->Slider->_getZoneOptionList();
		$this->Slider->setFieldProperties('zone',array('options'=>$zoneList));
		
		$sliderData = $this->Slider->findById($id);
		$this->pageTitle = sprintf(__("Edit %s"),$sliderData['Slider']['title']);
				
		if(empty($sliderData))
		{
			$this->errorMsg(__('Invalid URL'));
			$this->redirect('/admin/sliders');
		}
		if(!empty($this->data))
		{
			$this->Slider->id = $id;
                        $this->_processBanner();
			if($this->Slider->save($this->data))
			{
				$this->successMsg(__('Slider has been updated successfully!'));
				$this->redirect('/admin/sliders');
			}else
			{
				$this->errorMsg(__('Slider has not been updated. Please try again!'));
			}
		}
		if(empty($this->data))
		{
			$this->data = $sliderData;
		}
	}

	function admin_view($id = null)
	{
		$this->pageTitle = __("Sliders view");
		$sliderData = $this->Slider->findById($id);
		if(empty($sliderData))
		{
			$this->errorMsg(__('Invalid URL'));
			$this->redirect('/admin/sliders');
		}
		$this->set(compact('sliderData'));
	}
	
	function admin_delete($id = null)
	{
		$sliderData = $this->Slider->findById($id);
		if(empty($sliderData))
		{
			$this->errorMsg(__('Invalid URL'));
			$this->redirect('/admin/sliders');
		}
		
		$this->Slider->id =$id ;
		if($this->Slider->delete($id))
		{
			$this->successMsg(__('Slider has been deleted successfully!'));
		}
		else
		{
			$this->errorMsg(__('Unable to delete the specified ad.'));
		}
		$this->redirect('/admin/sliders');
	}

	function admin_random_view($zone = null)
	{
		$this->pageTitle = sprintf(__("Random view for zone %s"),Inflector::humanize($zone));
		if(empty($zone))
		{
			$this->errorMsg(__('Wrong parameter!'));
			$this->redirect('/admin/sliders/index');
		}
		$this->set(compact('zone'));
	}
	
	function admin_publish($id = null)
	{
		$sliderData = $this->Slider->findById($id);
		if(empty($sliderData))
		{
			$this->errorMsg(__('Invalid URL'));
			$this->redirect('/admin/sliders');
		}
		$this->Slider->id =$id;
		if($this->Slider->saveField('published',Slider::PUBLISHED))
		{
			$this->successMsg(__('Slider has been published.'));
		}else
		{
			$this->errorMsg(__('Sorry, Slider could not be published.'));
		}
		$this->redirect('/admin/sliders/index');

	}

	function admin_unpublish($id = null)
	{
		$sliderData = $this->Slider->findById($id);
		if(empty($sliderData))
		{
			$this->errorMsg(__('Invalid URL'));
			$this->redirect('/admin/sliders');
		}
		$this->Slider->id =$id;
		if($this->Slider->saveField('published',Slider::UNPUBLISHED))
		{
			$this->successMsg(__('Slider has been unpublished!'));
		}else
		{
			$this->errorMsg(__('Sorry, Slider could not be unpublished.'));
		}
		$this->redirect('/admin/sliders/index');

	}
	
	function admin_editZone()
	{
		$this->pageTitle = __("Edit Zone");
		//Zone is a configuration model.
		$this->Zone->cache = true;
		if(!empty($this->data))
		{
			if($this->data['Zone']['zone'])
			{
				$zoneOptions = explode("\n",$this->data['Zone']['zone']);
				$this->data['Zone']['zone'] = serialize($zoneOptions);
			}
			$this->Zone->create();
			if($this->Zone->save($this->data,array('validate'=>'first')))
			{
				//delete ad for zone that does not exists.
				$this->Slider->deleteAfterZoneEdit();
				$this->successMsg(__('Zone list has been updated successfully.'));
				$this->redirect('/admin/sliders');
			}else
			{
				$this->errorMsg(__('Sorry, zone list could not be updated.'));
			}
		}else
		{
			$this->data['Zone']['zone'] = implode("\n",$this->Slider->_getZoneOptionList());
		}
	}
        
        function _processBanner()
	{
		if(!empty($this->data))
		{	
			if(isset($this->data['Slider']['image']) && $this->data['Slider']['image']['name'] == '')
			{
				unset($this->data['Slider']['image']);
			}elseif(isset($this->data['Slider']['image']))
			{  
                                $source = $this->data['Slider']['image']['tmp_name'];
                                $destination = WWW_ROOT.DS.'uploads'.DS.'sliders'.DS.$this->data['Slider']['image']['name'];
                                if(move_uploaded_file($source, $destination))
                                {
                                        $logoImage = $this->data['Slider']['image']['name'];
                                        unset($this->data['Slider']['image']);
                                        $this->data['Slider']['image'] = $logoImage;
                                }
			}					
		}
	}
}
?>